package com.cqndt.visual.service.impl;

import com.cqndt.visual.service.BusinessService;
import com.cqndt.visual.socket.WebSocketServer;
import com.cqndt.visual.util.Result;
import com.cqndt.visual.vo.CaseVo;
import com.cqndt.visual.vo.FlightDataVo;
import com.cqndt.visual.vo.MessageVo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;


/**
 * Created By marc
 * Date: 2021/5/12  14:15
 * Description:
 */
@Service
public class BusinessServiceImpl implements BusinessService {

    @Override
    @Async
    public void fightData(String message) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            FlightDataVo vo = mapper.readValue(message,new TypeReference<FlightDataVo>(){});
            WebSocketServer.sendInfo(message, vo.getUavSn() + "-0");
            WebSocketServer.sendInfo(message, vo.getUavSn() + "-1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @Async
    public void fightDetailData() {
        ObjectMapper mapper = new ObjectMapper();
        String message = "{ " +
                "\"aiPeopleNum\":0, " +
                "\"code\":1, " +
                "\"airSpeed\":"+Math.random()*100+", " +
                "\"angle\":2.0865633, " +
                "\"appVersions\":\"2.1.0\", " +
                "\"attitude\":313.45502, " +
                "\"batteryWarning\":0, " +
                "\"current\":0, " +
                "\"electricQuantity\":0, " +
                "\"flyHeading\":0, " +
                "\"gpsNum\":\"0\", " +
                "\"landSpeed\":"+Math.random()*100+", " +
                "\"orienteering\":\"无效\", " +
                "\"pitchangle\":0, " +
                "\"podLatitude\":"+Math.random()*100+", " +
                "\"podLongitude\":"+Math.random()*100+", " +
                "\"pressure\":7, " +
                "\"remoteControlSignal\":0, " +
                "\"roll\":0, " +
                "\"temperature\":0, " +
                "\"uavFlyTime\":718, " +
                "\"uavFlyingHeight\":"+Math.random()*100+", " +
                "\"uavGpsTime\":1445109878, " +
                "\"uavLatitude\":"+Math.random()*100+", " +
                "\"uavLongitude\":"+Math.random()*100+", " +
                "\"uavModel\":\"E260-SECURITY\", " +
                "\"uavSn\":\"4MFCC12004003G\", " +
                "\"uavFirmwareVersion\":\"1.10.0 (1127)\", " +
                "\"verticalSpeed\":0.012, " +
                "\"videoSignal\":0, " +
                "\"voltage\":23.65966796875, " +
                "\"aiCarNum\":0, " +
                "\"yaw\":0 " +
                "}";
        while (true) {
                try {
                    FlightDataVo vo = mapper.readValue(message, new TypeReference<FlightDataVo>() {
                    });
                    WebSocketServer.sendInfo(message, vo.getUavSn() + "-0");
                    WebSocketServer.sendInfo(message, vo.getUavSn() + "-1");
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }

    }

    @Override
    @Async
    public void sendMessage(String uavSn) {
        try {
            Thread.sleep(20000);
            MessageVo vo = new MessageVo();
            System.out.println("发送消息");
            WebSocketServer.sendInfo(vo.toString(),uavSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Result publishTask(CaseVo caseVo) {
        Result result = new Result();
        try {
            WebSocketServer.sendInfo(caseVo.toString(),caseVo.getUavSn()+"-0");
        } catch (IOException e) {
            e.printStackTrace();
            result.failure(500,"发布任务失败");
        }
        return result;
    }
}
